package com.cssy.yd.basic.web;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.entity.BasicPlaceofstorageDO;
import com.cssy.yd.basic.entity.BasicStorageinfoDO;
import com.cssy.yd.basic.service.IBasicPlaceofstorageService;
import com.cssy.yd.basic.service.IBasicStorageService;

import com.plat.helper.JqGridHandler;

import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Controller
@RequestMapping("/yd/basic/hub")
public class PlaceofStorageController extends BaseController<BasicPlaceofstorageDO>{
	private static final String MANAGER = "/yd/basic/hub/HUBManager";//列表页面
	private static final String EIDT = "/yd/basic/hub/HUBOperation";//添加、修改、显示页面
	
	@Resource(name="basichubService")
	private IBasicPlaceofstorageService basichubService;
	
	@Resource(name="syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name="basicwhService")
	private IBasicStorageService basicwhService;

	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}
	
	
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
			 scs.setOrderBy("dctime desc");
		SearchResult result=	basichubService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		SearchConditions scs = new SearchConditions();
		SearchResult st = basicwhService.findListByExample(scs);
		List<BasicStorageinfoDO> bclist = st.getResults();
		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
			model.addAttribute("flag","add");
			model.addAttribute("bclist", bclist);
		}else{
			BasicPlaceofstorageDO hub=	basichubService.findListById(id);
			model.addAttribute("bclist", bclist);
			model.addAttribute("hub", hub);
			model.addAttribute("flag","edit");
		}
		return EIDT;
	}
	
	
	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicPlaceofstorageDO curAP, BindingResult result, Model model){
		BodyMsg bodyMap=BodyMsg.getInstance();
		try {
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				curAP.setDutime(new Date());
				basichubService.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(curAP.getId());
			}else{//新增
				curAP.setDctime(new Date());
				SearchConditions scs = new SearchConditions();
				SearchConditions scs1 = new SearchConditions();
				List<BasicPlaceofstorageDO> bdolist=null;
				if(curAP.getCode()!=null&&!"".equals(curAP.getCode())){
					scs.addCondition("code", curAP.getCode(), SearchCondition.OP_EQUAL);
					SearchResult rs = basichubService.findListByExample(scs);
					bdolist=rs.getResults();
					if(bdolist.size()>0){
						bodyMap.isFail();
						bodyMap.configWebInfor("编码已经存在请输入其他编码！");
						return bodyMap.modelMap;
					}
				}
				scs1.addCondition("storageid", curAP.getStorageid(), SearchCondition.OP_EQUAL);
				scs1.addCondition("storagename", curAP.getStoragename(), SearchCondition.OP_EQUAL);
				scs1.addCondition("name", curAP.getName(), SearchCondition.OP_EQUAL);
				SearchResult rs1 = basichubService.findListByExample(scs1);
				bdolist=rs1.getResults();
				if(bdolist.size()>0){
					bodyMap.isFail();
					bodyMap.configWebInfor("同一个仓库不可以有两个相同的库位！");
					return bodyMap.modelMap;
				}
				BasicPlaceofstorageDO cdo=basichubService.save(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(cdo.getId());
				
			}
			
		} catch (Exception e) {			
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
		
	}
		

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			
			for(String field : fields){
				basichubService.delete(field, SessionUtil.getUser(request));
			}
		
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}



}
